package com.a3dweijing.wetouch2.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.a3dweijing.wetouch2.Activity.VideoPlayActivity;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.PartitionRecItemAdapter;
import com.a3dweijing.wetouch2.bean.ExpoonVideo;
import com.a3dweijing.wetouch2.bean.PartitionTypeMsg;
import com.a3dweijing.wetouch2.bean.VideoType;
import com.a3dweijing.wetouch2.callback.PartitionOnclick;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.a3dweijing.wetouch2.recyclerviewdecoration.RecycleViewDivider;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yxd on 2018/2/7.
 */

public class PartitionVideoFragment extends Fragment implements PartitionOnclick {
    private static PartitionVideoFragment instance;
    private RecyclerView recyclerView;
    private Handler handler;
    private LinearLayout list_null;
    int pagecout=0,page=1;
    private List<ExpoonVideo> videos;
    private PartitionRecItemAdapter adapter;

    public PartitionVideoFragment() {
    }
    public static PartitionVideoFragment newinstance(String id)
    {
        Bundle bundle=new Bundle();
        bundle.putString("id",id);
        instance=new PartitionVideoFragment();
        instance.setArguments(bundle);
        return instance;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view=inflater.inflate(R.layout.fragment_partition_video,container,false);
        recyclerView= (RecyclerView) view.findViewById(R.id.partition_video_rec);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        recyclerView.addItemDecoration(new RecycleViewDivider(10, Color.WHITE));
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!ViewCompat.canScrollVertically(recyclerView, 1))
                {
                    if (pagecout>=page)
                    {
                        new GetPartitiontypeThread().start();
                    }else {
//                        Toast.makeText(getContext(),"没有更多内容了", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        list_null= (LinearLayout) view.findViewById(R.id.resaut_status);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what)
                {
                    case 100:
                        PartitionTypeMsg myVideoMsg=new Gson().fromJson(msg.obj.toString(),PartitionTypeMsg.class);
                        if (myVideoMsg!=null && myVideoMsg.isFalg())
                        {
                            pagecout=myVideoMsg.getPageCount();
                            if (myVideoMsg.getVideoList1()!=null && myVideoMsg.getVideoList1().size()>0)
                            {
                                videos=new ArrayList<>();
                                videos.addAll(myVideoMsg.getVideoList1());
                                recyclerView.setAdapter(adapter=new PartitionRecItemAdapter(videos,PartitionVideoFragment.this));
                                list_null.setVisibility(View.GONE);
                            }else {
                                list_null.setVisibility(View.VISIBLE);
                            }
                        }
                        page++;
                        break;
                    case 200:
                        PartitionTypeMsg myVideoMsg2= new Gson().fromJson(msg.obj.toString(), PartitionTypeMsg.class);
                        if (myVideoMsg2 != null && myVideoMsg2.isFalg()) {
                            videos.addAll(myVideoMsg2.getVideoList1());
                            adapter.notifyDataSetChanged();
                            page++;
                        }
                        break;
                    case 404:
                        Toast.makeText(getContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        };
        new GetPartitiontypeThread().start();
        return view;
    }



    @Override
    public void typeclick(int position, VideoType type) {

    }

    @Override
    public void videoclick(int position, Object o) {
        Intent intent=new Intent();
        intent.setClass(getContext(), VideoPlayActivity.class);
        ExpoonVideo video= (ExpoonVideo) o;
        intent.putExtra("video",video);
        startActivity(intent);

    }

    private class GetPartitiontypeThread extends Thread {
        String type_id;
        String pageNo;

        public GetPartitiontypeThread() {
            this.type_id = getArguments().getString("id");
            this.pageNo = page+"";
        }

        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotGetPartitionType(type_id,page+"");
            if (resulet != null && resulet.length() > 7) {
                if (page==1)
                {
                    handler.sendMessage(handler.obtainMessage(100, resulet));
                }else {
                    handler.sendMessage(handler.obtainMessage(200, resulet));
                }

            }else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }
    }

}
